#if ENABLE_SDKUTIL_DOUYIN && !UNITY_EDITOR
#define ENABLE_SDKUTIL_DOUYIN2
#endif

#if ENABLE_SDKUTIL_DOUYIN2
using TTSDK;
using TTSDK.UNBridgeLib.LitJson;
using static TTSDK.TTAndroidCallJsInterface;
#endif

using System.Collections;
using UnityEngine;
using wnity.zframework;
using System.Collections.Generic;
using System;


namespace cframework.SDKUtil
{

    public class SDKUtil_DouYin : ISDKUtil
    {
        public static SDKUtil_DouYin _instance;

        public static SDKUtil_DouYin instance
        {
            get
            {
#if ENABLE_SDKUTIL_DOUYIN2
                if (_instance == null) _instance = new();
#endif
                return _instance;
            }
        }

        private SDKUtil_DouYin() { }


        public bool isInited { get; private set; }


        public void SetOnShow(Action<Dictionary<string, object>> onShowCallback)
        {
#if ENABLE_SDKUTIL_DOUYIN2
            var lifeCycle = TT.GetAppLifeCycle();
            lifeCycle.OnShow += (res) =>
            {
                onShowCallback?.Invoke(res);
            };
#endif
        }

        public IEnumerator CheckScene()
        {
#if ENABLE_SDKUTIL_DOUYIN2
            bool retSuccess = false;
            int retErrCode = 0;
            string retErrMsg = null;

            bool callbacked = false;
            TT.CheckScene(TTSDK.TTSideBar.SceneEnum.SideBar, success =>
            {
                callbacked = true;
                retSuccess = success;
            }, null, (errCode, errMsg) =>
            {
                callbacked = true;
                retErrCode = errCode;
                retErrMsg = errMsg;
            });

            yield return new WaitUntil(() => callbacked);

            yield return (retSuccess, retErrCode, retErrMsg);
#endif
            yield return (false, 0, (string)null);
        }



        public void NavigateToScene(string scene)
        {
#if ENABLE_SDKUTIL_DOUYIN2
            JsonData data = new JsonData();
            data["scene"] = scene;
            TT.NavigateToScene(data, null, null, null);
#endif
        }

        public IEnumerator InitSDK()
        {
#if ENABLE_SDKUTIL_DOUYIN2
            var initResult = new ISDKUtil.InitResult();
            if (isInited)
            {
                initResult.success = true;
                yield return initResult;
            }

            var callbacked = false;
            TT.InitSDK((int code, ContainerEnv env) =>
            {
                callbacked = true;
                initResult.code = code;
                initResult.success = code == 200;
            });


            yield return new WaitUntil(() => callbacked);

            isInited = initResult.success;

            yield return initResult;
#else
            yield return new ISDKUtil.InitResult();
#endif
        }


        public IEnumerator Login(double timeout = 0) //timeout��λms
        {
#if ENABLE_SDKUTIL_DOUYIN2

            var loginResult = new ISDKUtil.LoginResult();
            var completed = false;

            TT.Login((string code, string anonymousCode, bool isLogin) =>
            {
                completed = true;

                loginResult.success = true;
                loginResult.code = code;

            }, (string errMsg) =>
            {
                completed = true;

                loginResult.success = false;
                loginResult.errMsg = errMsg;

            });

            yield return new WaitUntil(() => completed);

            yield return loginResult;
#else
            yield return new ISDKUtil.LoginResult();
#endif
        }

    }
}